<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<form id="menu-add-form" method="post" style="padding: 10px;">  
	<input name="pid" type="hidden" value="" /> 
    <div>   
        <label for="name">名称:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input class="easyui-validatebox" type="text" name="name"
         data-options="required:true" onblur="menu.checkName(this.value)" />
        <span id="name-tip"></span>
    </div>
    <br />  
     <div>   
        <label for="url">url:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input type="text" name="url" />
    </div>    
</form>  

<script type="text/javascript">
	menu.add=function(){
		//初始化pid
		$('#menu-add-form input[name="pid"]').val(menu.pid);
		
		console.info("add() --->  "+menu.pid);
		
		$('#menu-add-form').form('submit', {
			url: '/menu/add',
			onSubmit: function(){
				var isValid = $(this).form('validate');
				var name = $('#menu-add-form input[name="name"]').val();
				var flag = menu.checkName(name);
				return isValid && flag;	// 返回false终止表单提交
			},
			success: function(data){
				//把字符串转化成 json对象
				var data = JSON.parse(data);
				if(data.code == 200){
					//添加成功后，更新树形
					var selected = $('#menu-list-tree').tree('getSelected');
					$('#menu-list-tree').tree('append', {
						parent: selected?selected.target:null, //如果为空代表是添加根节点，否则添加选择节点的子节点
						data: [{
							id: data.data.id,
							text: data.data.name
						}]
					});
					$.messager.alert('提示','添加成功！','info');
				}
			}
		});
	}
	
	menu.checkName=function(name){
		var name = $.trim(name);
		if(name == ''){
			return;
		}
		//名称被占用
		var flag= false;
		
		//改成同步
		$.ajax({
		   type: "get",
		   async:false,
		   url: "/menu/name/check",
		   data: "name="+name,
		   success: function(msg){
		     if(msg.code == 200 && !msg.data){
		    	 $('#name-tip').html("<font color='green'>该名称可以使用</font>");
		    	 flag = true;
		     }else if(msg.code == 200 && msg.data){
		    	 $('#name-tip').html("<font color='red'>该名称已被使用</font>");
		     }
		   }
		});
		
		return flag;
	}
</script>